<!--
 Copyright (C) 2020 Tencent Cloud.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<!--{template common/header}-->
<style type="text/css">
    .verify-code-btn {
        color: #ffffff;
        text-align: center;
        background-color: #2ea2d9;
    }

    .verify-code-btn:hover {
        cursor: pointer;
    }

    /* 按钮disabled的样式 */
    button[disabled], .verify-code-btn[disabled]:not([type]) {
        color: #2ea2d9;
        border: none;
        background-color: #ededed;
    }
</style>
<h3 class="flb">
    <em>{lang tencentcloud_sms:phone_reset}</em>
    <span class="flbc" onclick="hideWindow('login', 0, 1)">X</span>
</h3>
<form class="cl" data-referer="{echo dreferer();}">
    <div class="c cl">
        <input type="hidden" name="formhash" value="{FORMHASH}"/>
        <input type="hidden" name="handlekey" value="login"/>
        <div class="rfm">
            <table>
                <tr>
                    <th><label for="phone">{lang tencentcloud_sms:phone}:</label></th>
                    <td><input type="text" name="phone" id="phone"  size="28" class="px p_fre"/></td>
                    <td class="tipcol"><a href="member.php?mod=register">{lang tencentcloud_sms:reg}</a></td>
                </tr>
            </table>
        </div>
        <div class="rfm">
            <table>
                <tr>
                    <th><label for="verify-code">{lang tencentcloud_sms:verify_code}:</label></th>
                    <td><input type="text" id="verify-code" name="verify-code" autocomplete="off" size="15"
                               class="px p_fre" style="width:6rem;"/></td>
                    <td>
                        <button type="button" class="verify-code-btn" id="get-verify-code">{lang tencentcloud_sms:send_verify_code}</button>
                    </td>
                </tr>
            </table>
        </div>
        <div class="rfm">
            <table>
                <tr>
                    <th><label for="new-pwd">{lang tencentcloud_sms:new_pwd}:</label></th>
                    <td><input type="password" id="new-pwd" size="28" class="px p_fre"></td>
                </tr>
            </table>
        </div>
        <div class="rfm">
            <table>
                <tr>
                    <th><label for="confirm-new-pwd">{lang tencentcloud_sms:confirm_new_pwd}:</label></th>
                    <td><input type="password" id="confirm-new-pwd" size="28" class="px p_fre"></td>
                </tr>
            </table>
        </div>
        <!--{if $seccodecheck}-->
        <!--{block sectpl}--><div class="rfm"><table><tr><th><sec>: </th><td><sec><br /><sec></td></tr></table></div><!--{/block}-->
        <!--{subtemplate common/seccheck}-->
        <!--{/if}-->
        <div class="rfm bw0">
            <table>
                <tr>
                    <th></th>
                    <td>
                        <button type="button" id="reset-btn" class="pn pnc"><strong>{lang tencentcloud_sms:submit}</strong></button>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</form>
<script src="source/plugin/tencentcloud_sms/js/jquery.min.js"></script>
<script type="text/javascript">
    jQuery.noConflict();
    jQuery(function ($) {
        //倒计时
        var waitTime = 60;
        //ajax请求地址
        var ajaxUrlPrefix = 'plugin.php?id=tencentcloud_sms:';

        function sendCountdown() {
            if (waitTime > 0) {
                $('#get-verify-code').text(waitTime + '{lang tencentcloud_sms:flush_code}').attr("disabled", true);
                waitTime--;
                setTimeout(sendCountdown, 1000);
            } else {
                $('#get-verify-code').text('{lang tencentcloud_sms:get_verify_code}').attr("disabled", false).fadeTo("slow", 1);
                waitTime = 60;
            }
        }

        //获取验证码
        $("#get-verify-code").click(function () {
            var phone = $("#phone").val();
            //校验手机号
            if (phone === '' || !phone.match(/^1[3-9]\d{9}$/)) {
                showDialog('{lang tencentcloud_sms:phone_error}', 'error', 'warning', function () {
                    $("#phone").focus();
                });
                return;
            }
            //ajax请求
            $.ajax({
                type: "post",
                url: ajaxUrlPrefix + 'get_verify_code',
                dataType: "json",
                data: {
                    phone: phone,
                    type: 3,
                },
                success: function (response) {
                    if (response.code !== 0) {
                        showDialog(response.msg, 'error', 'error Tips');
                        return;
                    }
                    sendCountdown();
                }
            });
        });
        //登录
        $("#reset-btn").click(function () {
            var phone = $("#phone").val();
            var verifyCode = $("#verify-code").val();
            var newPwd = $("#new-pwd").val();
            var confirmNewPwd = $("#confirm-new-pwd").val();
            var secCode = $("input[name='seccodeverify']").val();
            var secCodeHash = $("input[name='seccodehash']").val();
            //校验手机号
            if (phone === '' || !phone.match(/^1[3-9]\d{9}$/)) {
                showDialog('{lang tencentcloud_sms:phone_error}', 'error', 'warning', function () {
                    $("#phone").focus();
                });
                return;
            }
            //校验短信验证码
            if (verifyCode === '' || verifyCode.length < 4) {
                showDialog('{lang tencentcloud_sms:code_error}', 'error', 'warning', function () {
                    $("#verify-code").focus();
                });
                return;
            }
            //校验新密码
            if (newPwd === '' || newPwd !== confirmNewPwd) {
                showDialog('{lang tencentcloud_sms:two_pwd_diff}', 'error', 'warning', function () {
                    $("#new-pwd").focus();
                });
                return;
            }

            $.ajax({
                type: "post",
                url: ajaxUrlPrefix + 'reset_pwd_by_phone&seccodehash=' + secCodeHash,
                dataType: "json",
                data: {
                    phone: phone,
                    verifyCode: verifyCode,
                    secCode: secCode,
                    newPwd: newPwd,
                    confirmNewPwd: confirmNewPwd,
                },
                success: function (response) {
                    if (response.code !== 0) {
                        showDialog(response.msg, 'error', 'error');
                        return;
                    }
                    showDialog('{lang tencentcloud_sms:reset_success}', 'right', 'success', function () {
                        window.location.href = response.data.location;
                    });
                }
            });
        });
    });
</script>
<!--{template common/footer}-->